<?php
header("content-type:text/html;charset=utf8");
$mid = isset($_GET['mid'])?$_GET['mid']:'';
$uid = isset($_GET['uid'])?$_GET['uid']:'';
$appId = isset($_GET['appId'])?$_GET['appId']:'';
$return = array();
if (!$mid) {
	$return['code'] = 10001;
	$return['data'] = '缺少必要参数，请进行扫码重试';

}else if (!$uid && !$appId) {
	$return['code'] = 10001;
	$return['data'] = '缺少必要参数，请进行扫码重试';

}else if ($uid) {
	//pdo连接数据库
	$pdo = new PDO('mysql:host=123.57.16.97;dbname=project','root','zqVnUy6cxsQXbpEZ');
	$pdo->exec("set names utf8");
	//根据mid获取电影信息
	$movieInfo = getMovieInfo($pdo,$mid);
	if ($movieInfo) {
		$userInfo = getUserInfo($pdo,$uid);
		$movieInfo['user'] = $userInfo; 
		$return['code'] = 10000;
		$return['data'] = $movieInfo;
	}else{
		$return['code'] = 10002;
		$return['data'] = '缺少电影详细信息，请扫码重试';
	}

}else if ($appId) {
	//只传递了一个appId跳转页面完成登录操作，并展示
	$url = "https://pay.hispot.cn/app/getWxInfo.php?appId=".$appId."&mid=".$mid;
	header('location:'.$url);
	exit;
}

if ($return['code'] == 10000) {
	//信息完整，进入确认订单页面
	$url = 'https://pay.hispot.cn/app/h5/movieDetails.php?data='.json_encode($return['data']);
    header('location:'.$url);
}else{
	//信息不完整。
	header("location:https://pay.hispot.cn/app/h5/error.html");
}

//通过应用ID获取登录信息的uid
function getUid($pdo,$appId){
	$sql = "SELECT uid FROM relation WHERE appid = '".$appId."' and status = 1";
	$uid = $pdo->query($sql)->fetch(PDO::FETCH_ASSOC);	
	return $uid['uid'];
}

//通过mid获取对应的电影详情
function getMovieInfo($pdo,$mid){
	$sql = "SELECT mid,name,price,poster FROM movie WHERE mid = '".$mid."'";
	$movieInfo = $pdo->query($sql)->fetch(PDO::FETCH_ASSOC);
	return $movieInfo;
}

function curl_get_https($url){
    $curl = curl_init(); // 启动一个CURL会话
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_HEADER, 0);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, true);  // 从证书中检查SSL加密算法是否存在
    $tmpInfo = curl_exec($curl);     //返回api的json对象
    //关闭URL请求
    curl_close($curl);
    return $tmpInfo;    //返回json对象
}

function getUserInfo($pdo,$uid){
	$sql = "SELECT uid,nickname,headimgurl,openid FROM `user` WHERE uid = ".$uid;
  	$userInfo = $pdo->query($sql)->fetch(PDO::FETCH_ASSOC);
  	return $userInfo;
}
 ?>
